package com.qffx.moviebase.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.qffx.moviebase.R;
import com.qffx.moviebase.bean.classify.ClassifyEntity;
import com.qffx.moviebase.view.classify.SpecialWebviewActivity;

import java.util.List;

/**
 * Created by lenovo on 2017/2/8.
 */

public class AuthorListAdapter extends RecyclerView.Adapter<AuthorListAdapter.ViewHolder> {
    private List<ClassifyEntity.AuthorListBean> list;
    private Context context;
    private LayoutInflater inflater;

    public AuthorListAdapter(List<ClassifyEntity.AuthorListBean> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_anthor_iv, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tv1.setText(list.get(position).getNickname());
        holder.tv2.setText(list.get(position).getDescription());

        //原图基础上复合变换成圆图 +毛玻璃（高斯模糊）

        //Glide.with(this).load(url).bitmapTransform(new BlurTransformation(this, 25), new CropCircleTransformation(this)).crossFade(1000).into(image4);

        // Glide.with(context).load(list.get(position).getLogo()).bitmapTransform(new CropCircleTransformation(this)).crossFade(1000).into(holder.iv);
        Glide.with(context).load(list.get(position).getLogo()).asBitmap().centerCrop().into(new BitmapImageViewTarget(holder.iv) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                holder.iv.setImageDrawable(circularBitmapDrawable);
            }
        });

        holder.clickMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SpecialWebviewActivity.class);
                intent.putExtra("url", "http://www.moviebase.cn/uread/app/topic/shareViewTopic-" + list.get(position).getId() + ".html");
                context.startActivity(intent);

            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv;
        private TextView tv1, tv2;
        private RelativeLayout clickMore;

        public ViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.classify_author_iv);
            tv1 = (TextView) itemView.findViewById(R.id.author_tv1);
            tv2 = (TextView) itemView.findViewById(R.id.author_tv2);
            clickMore = (RelativeLayout) itemView.findViewById(R.id.author_click);


        }
    }

}
